Ext.onReady(function() {
	var editForm = new Ext.FormPanel({
		labelAlign: 'right',
		region: 'center',
		autoScroll:true, 
		labelWidth: 100,
		frame: true,
		xtype: 'fieldset',
		items: [
			{
				items: [
					{
						columnWidth: .01, layout: 'form',
						items: [
							{ xtype: 'hidden', name: 'id', hidden:true, hiddenLabel:true}
						]
					}
				]
			},
			{
				items: [
					{
						layout: 'form',
						items: [
							{xtype: 'textfield',name: 'icon', fieldLabel: '图标',anchor : '95%'}
						]
					},
					{
						layout: 'form',
						items: [
							{xtype: 'textfield',name: 'name', fieldLabel: '名称',anchor : '95%'}
						]
					},
					{
						layout: 'form',
						items: [
							{xtype: 'textfield',name: 'urlPath', fieldLabel: '路径',anchor : '95%'}
						]
					}
				]
			}
		],
		buttons: [
			{
				text: '保存'	,
				handler: function() {
					if(!editForm.getForm().isValid())
						return;
					
					editForm.form.doAction('submit', {
						url: '/EWeb/HTML/basic/UserShortcutsAction/save.action',
						method: 'post',
						waitTitle:'请等待',
						waitMsg: '正在提交...',
						params: '',
						success: function(form, action) {
							if(action.result.result == 'success') {
								Ext.MessageBox.alert('结果', '保存成功！');
								form.reset();
								grid.getStore().reload();
								editWin.hide();
							}
						}
					});
				}
			},
			{
				text: '取消'	,
				handler: function() {
					editForm.form.reset();
					editWin.hide();
				}
			}
		]
	});
	var editWin = new Ext.Window({
		title: '编辑',
		modal: true,
		layout:'fit',
		width:450,
		height:180,
		closeAction:'hide',
		plain: true,
		layout: 'border',
		items: [editForm]
	});
	
	var searchForm = new Ext.FormPanel({
		frame: true,
		title: '查询',
		collapsible : true,
		collapsed: true,
		autoHeight:true,
		collapseMode:'mini',
		region: 'north',
		labelAlign: 'right',
		items: [
			{
				layout: 'column',
				items: [
					{
						columnWidth: .33, layout: 'form',
						items: [
							{xtype: 'textfield',name: 'icon', fieldLabel: '图标',anchor : '95%'}
						]
					},
					{
						columnWidth: .33, layout: 'form',
						items: [
							{xtype: 'textfield',name: 'name', fieldLabel: '名称',anchor : '95%'}
						]
					},
					{
						columnWidth: .33, layout: 'form',
						items: [
							{ xtype: 'textfield', name: 'urlPath', fieldLabel: '路径',anchor : '95%'}
						]
					}
				]
			}
		],
		buttons: [
			new Ext.Button({
				text: '查询',
				width: 70,
				handler: function() {
					var fv = searchForm.getForm().getValues();
					ds.baseParams= fv;
					ds.load({params: {start:0, limit:20}});
				}
			}),
			new Ext.Button({
					text: '清空',
					width: 70,
					handler: function() {
						searchForm.form.reset();
						ds.baseParams= {};
						ds.load({params: {start:0, limit:20}});
					}
			})
		]
	});
	
	var cm = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer(),
		{header:'图标', dataIndex:'icon', sortable:true},
		{header:'名称', dataIndex:'name', sortable:true},
		{header:'路径', dataIndex:'urlPath', sortable:true}
	]);
	
	var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/EWeb/HTML/basic/UserShortcutsAction/search.action'}),
		remoteSort: false,
		reader: new Ext.data.JsonReader(
			{
				totalProperty: 'total',
				idProperty:'id',
				root: 'invdata'
			},
			[
				{name: 'id'},
				{name: 'userId'},
				{name: 'icon'},
				{name: 'name'},
				{name: 'urlPath'}
			]
		)
	});
	ds.load({params: {start:0, limit:20}});
	var grid = new Ext.grid.GridPanel({
		region: 'center',
		ds: ds,
		cm: cm,
		viewConfig: {
			forceFit: true
		},
		tbar: new Ext.Toolbar({
			buttons: [
				{
					text: '查询'	,
					iconCls: 'find',
					handler: function() {
						if(searchForm.collapsed)
							searchForm.expand();
						else
							searchForm.collapse();
					}
				},
				{
					text: '新增',
					iconCls: 'add',
					handler: function() {
						editForm.form.reset();
						editWin.show();
					}
				},
				{
					text: '修改'	,
					iconCls: 'edit',
					handler: function() {
						var rs = grid.getSelectionModel().getSelected();
						editForm.load({
							url: '/EWeb/HTML/basic/UserShortcutsAction/getDetailInfo.action',
							params: {id: rs.data.id}
						});
						editWin.show();
					}
				},
				{
					text: '删除'	,
					iconCls: 'del',
					handler: function() {
						var rs = grid.getSelectionModel().getSelected();
						Ext.MessageBox.confirm('提示', '确定删除  ' + rs.data.name + ' ？', function(btn) {
							if(btn != 'yes') {
								return;
							}
							Ext.Ajax.request({
								method: 'post',
								url: '/EWeb/HTML/basic/UserShortcutsAction/delete.action',
								params: {id: rs.data.id},
								success:function(resp){
									var obj=Ext.util.JSON.decode(resp.responseText);
									if(obj.result == 'success') {
										grid.getStore().reload();
										Ext.MessageBox.alert('提示', '删除成功！');
									}
									else {
										Ext.MessageBox.alert('报错了！！！', '删除失败！！！' + obj.info);
									}
								}
							});
						});
					}
				}
			]
		}),	
		bbar: new Ext.PagingToolbar({
			pageSize: 20,
			store: ds,
			displayInfo: true,
			displayMsg: '显示第{0}条到{1}条记录,一共{2}条',
			emptyMsg: '没有记录'
		})
	});

	new Ext.Viewport({
	layout: 'border',
		items:[
			searchForm,grid
		]
	});
});